package com.swiftops.basic.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.swiftops.basic.sys.dao.SysDictMapper;
import com.swiftops.basic.sys.model.SysDict;
import com.swiftops.basic.sys.service.SysDictService;
import com.swiftops.basic.sys.vo.entity.SysDictEntityVO;
import com.swiftops.basic.sys.vo.request.SysDictRequestVO;
import com.swiftops.basic.sys.vo.response.SysDictResponseVO;
import com.swiftops.common.constant.CommonConstant;
import com.swiftops.common.constant.CommonEnum;
import com.swiftops.common.service.AbstractService;
import com.swiftops.common.core.PageResult;
import com.swiftops.common.util.IdGen;
import com.swiftops.common.util.ListUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;

/**
 * 字典
 *
 * @author yangyang
 * @date 2023/09/17
 **/
@Slf4j
@Service
@Transactional
public class SysDictServiceImpl extends AbstractService<SysDict> implements SysDictService {

    @Autowired
    private SysDictMapper sysDictMapper;

    /**
     * 查询所有字典
     *
     * @return
     */
    @Override
    public List<SysDict> findAll() {
        Condition condition = new Condition(SysDict.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("useable", "1");
        criteria.andEqualTo("deleteFlag", "0");
        List<SysDict> sysDictList = this.selectByCondition(condition);
        return sysDictList;
    }

    /**
     * 获取字典分页列表
     *
     * @param sysDictRequestVO 字典查询条件
     * @return pageResult 字典分页结果
     */
    @Override
    public PageResult<SysDictResponseVO> querySysDictPageList(SysDictRequestVO sysDictRequestVO) {
        //
        if (sysDictRequestVO.getPageNumber() == null) sysDictRequestVO.setPageNumber(CommonConstant.DEFAULT_PAGENUMBER);
        if (sysDictRequestVO.getPageSize() == null) sysDictRequestVO.setPageSize(CommonConstant.DEFAULT_PAGESIZE);
        //
        List<SysDictResponseVO> sysDictResponseVOList = Collections.emptyList();
        Condition condition = new Condition(SysDict.class);
        Example.Criteria criteria = condition.createCriteria();
        //
        //if (StringUtils.isNotBlank(sysDictRequestVO.getDesc())) {
        //    criteria.andLike("userName", "%" + sysDictRequestVO.getDesc() + "%");
        //}
        //删除状态
        criteria.andEqualTo("deleteFlag", CommonEnum.StateFlagEnum.STATE_FLAG_NORMAL.getCode());
        //
        int total = this.selectCountByCondition(condition);
        if (total > 0) {
            PageHelper.startPage(sysDictRequestVO.getPageNumber(), sysDictRequestVO.getPageSize());
            List<SysDict> sysDictList = this.selectByCondition(condition);
            sysDictResponseVOList = ListUtil.convertList(sysDictList, SysDictResponseVO.class);
        }
        //
        return PageResult.of(total, sysDictResponseVOList);
    }

    /**
     * 获取字典详细信息
     *
     * @param id 字典ID
     * @return 字典
     */
    @Override
    public SysDictEntityVO getSysDictEntityVO(String id) {
        SysDict sysDict = this.selectByPrimaryKey(id);
        if (null == sysDict) return null;

        SysDictEntityVO sysDictEntityVO = new SysDictEntityVO();
        BeanUtils.copyProperties(sysDict, sysDictEntityVO);
        return sysDictEntityVO;
    }

    /**
     * 添加字典
     *
     * @param sysDictEntityVO 字典
     */
    @Override
    public void addSysDict(SysDictEntityVO sysDictEntityVO) {
        SysDict sysDict = new SysDict();
        BeanUtils.copyProperties(sysDictEntityVO, sysDict);
        sysDict.setId(IdGen.next());
        this.insertSelective(sysDict);
    }

    /**
     * 修改字典
     *
     * @param sysDictEntityVO 字典
     */
    @Override
    public void updateSysDict(SysDictEntityVO sysDictEntityVO) {
        SysDict sysDict = new SysDict();
        BeanUtils.copyProperties(sysDictEntityVO, sysDict);
        this.updateByPrimaryKeySelective(sysDict);
    }

    /**
     * @param id        主键
     * @param isUseable 启用状态
     */
    @Override
    public void useableSysDict(String id, Boolean isUseable) {
        SysDict sysDict = new SysDict();
        sysDict.setId(id);
        Byte useable = isUseable ? CommonEnum.EnableStateEnum.ENABLE_STATE_YES.getCode() : CommonEnum.EnableStateEnum.ENABLE_STATE_NO.getCode();
        sysDict.setUseable(useable);
        this.updateByPrimaryKeySelective(sysDict);
    }

    /**
     * 删除字典
     *
     * @param id 字典ID
     */
    @Override
    public void deleteSysDict(String id) {
        SysDict sysDict = new SysDict();
        sysDict.setId(id);
        //sysDict.setStateFlag(CommonEnum.StateFlagEnum.STATE_FLAG_DELETE.getCode());
        //逻辑删除
        this.updateByPrimaryKeySelective(sysDict);
    }
}
